<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <title>Title</title>
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">学号</label>
        <div class="layui-input-block">
            <input type="text" name="stuNo" id="stuNo" disabled="disabled" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="stuName" id="stuName" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">班级</label>
        <div class="layui-input-block">
            <select name="classes" id="classes" lay-verify="required">
                <option value="1808041">1808041</option>
                <option value="1808042">1808042</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">专业</label>
        <div class="layui-input-block">
            <select name="department" id="department" lay-verify="required">
                <option value="软件工程">软件工程</option>
                <option value="计算机科学与技术">计算机科学与技术</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">电话</label>
        <div class="layui-input-block">
            <input type="text" name="tel" id="tel" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">宿舍</label>
        <div class="layui-input-block">
            <select name="dormno" id="dormno" lay-verify="required">
                <option value="20617">20617</option>
                <option value="10532">10532</option>
                <option value="10405">10405</option>
                <option value="10425">10425</option>
                <option value="3418">3418</option>
                <option value="3419">3419</option>
                <option value="10408">10408</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <select name="gender" id="gender" lay-verify="required">
                <option value="男">男</option>
                <option value="女">女</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <img src="">
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" id="updataMsg" lay-submit lay-filter="formDemo">修改信息</button>
            <button class="layui-btn layui-btn-danger" id="deleteUser" lay-submit lay-filter="formDemo">删除用户</button>
        </div>
    </div>
</form>

<script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
<script src="layui/layui.js"></script>
<script>
    $(function () {
        $.ajax({
            url:"/studentadminsystem/StudentMsgByStuNoServlet"
            ,type:"post"
            ,async:false/*要加个异步锁，不然有些数据还没获取到，select的值无法修改*/
            ,data:{
                No:getQueryVariable("stuNo")
            }
            ,cache: true
            ,success:function (data) {
                //判断是否为json格式
                if((typeof data=='object')&&data.constructor==Object){
                    obj=data;
                }else{
                    obj  = eval("("+data+")");
                }
                //alert(data)
                //alert(obj[0].stuNo)
                setImformation(obj)
            }
        });

        //更改用户信息
        $("#updataMsg").on("click",function () {
            //前端判别信息合法性还没做
            //
            $.ajax({
                url:"/studentadminsystem/UpdateStudentServlet"
                ,type:"post"
                ,data:{
                    stuNo:$("#stuNo").val()
                    ,stuName:$("#stuName").val()
                    ,classes:$("#classes").val()
                    ,department:$("#department").val()
                    ,dormno:$("#dormno").val()
                    ,tel:$("#tel").val()
                    ,gender:$("#gender").val()
                }
                ,cache: true
                ,success:function (data) {
                    //判断是否为json格式
                    if((typeof data=='object')&&data.constructor==Object){
                        obj=data;
                    }else{
                        obj  = eval("("+data+")");
                    }
                    alert(obj.result)
                }
                ,error:function () {
                //还有失败的没有做
                }
            });
        })


        //删除用户信息
        $("#deleteUser").on("click",function () {
            //隐藏当前用户删除按钮的功能没写
            $.ajax({
                url:"/studentadminsystem/DeleteUserServlet"
                ,type:"post"
                ,data:{
                    stuNo:$("#stuNo").val()
                }
                ,async:false
                ,cache: true
                ,success:function (data) {
                    alert("ABC")
                    alert(data)
                    //判断是否为json格式
                    if((typeof data=='object')&&data.constructor==Object){
                        obj=data;
                    }else{
                        obj=eval("("+data+")");
                    }
                    alert("成功")
                    //alert(obj)
                  //  alert(obj[0].result)
                    if(obj.result=="ok")
                        alert("删除成功")
                    else
                        alert("删除失败")
                }
            })
        })

    })

    //设置表格信息
    function setImformation(obj){
        $("#stuNo").val(obj[0].stuNo)
        $("#stuName").val(obj[0].stuName)
        $("#classes").val(obj[0].classes)
        $("#department").val(obj[0].department)
        $("#tel").val(obj[0].tel)
        $("#dormno").val(obj[0].dormNo)
        $("#gender").val(obj[0].gender)
    }


    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });

    //获取当前页面url的参数
    function getQueryVariable(variable)
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

</script>
</body>
</html>